package routers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

/*注册路由*/
func RegisterRouter() *gin.Engine {
	r := gin.Default()

	registerWelcomeApi(r)
	registerUserApi(r)

	return r
}

func registerWelcomeApi(r *gin.Engine) {
	// 绑定路由规则，执行函数，gin.Context，封装了request和response
	// 0.第一个GET请求测试接口：HelloWorld
	// 不含参数 http://localhost:8080/
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello World!")
	})
	// 含API参数 http://localhost:8080/welcome/SuperMan
	r.GET("/welcome/*name", func(c *gin.Context) {
		//通过Param可取API参数
		name := c.Param("name")
		name = strings.Trim(name, "/")
		c.String(http.StatusOK, "Hello World! Let`s Go, %s !", name)
	})
	// 含URL参数 http://localhost:8080/welcome?name=SuperWoman
	r.GET("/welcome", func(c *gin.Context) {
		//通过Query可取URL参数
		name := c.DefaultQuery("name", "No one")
		c.String(http.StatusOK, "Hello World! Let`s Go, %s !", name)
	})
}
